<%@page import="study.itwill.team5.house.HouseItem"%>
<%@page import="study.itwill.team5.house.House"%>
<%@page import="study.itwill.team5.admin.AdminItem"%>
<%@page import="study.itwill.jsp.WebHandler"%>
<%@page import="study.itwill.team5.board.BoardItem"%>
<%@page import="study.itwill.team5.board.Board"%>
<%@page import="study.itwill.team5.member.MemberItem"%>
<%@page import="study.itwill.team5.member.Member"%>
<%@page import="study.itwill.team5.board.BoardCategoryItem"%>
<%@page import="study.itwill.team5.board.BoardCategory"%>
<%@page import="java.util.ArrayList"%>
<%@page import="study.itwill.common.DBHandler"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ include file="../session.jsp"%>
<%@ include file="../dbcon.jsp"%>
<%

	int board_seq = web.getInt("board_seq");
	
	if (board_seq == -1) {
		db.disconnect();
		web.goPage(null, "파라미터가 잘못되었습니다.");
		return;
	}

	// 게시판 카테고리에 대한 ArrayList 받아오기
	BoardCategory category = new BoardCategory();
	ArrayList<BoardCategoryItem> category_list = category.listBoardCategory();
	
	// 회원목록 받아오기
	Member member = new Member();
	ArrayList<MemberItem> member_list = member.listMember(0);
	
	// 민박목록 받아오기
	House house = new House();
	ArrayList<HouseItem> house_list = house.listHouse(0, null);
	
	/** 게시판 조회 */
	Board board = new Board();
	BoardItem b_item = board.viewBoard(board_seq);
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<%@ include file="../head.jsp"%>
</head>
<body>
	<%@ include file="../menu.jsp"%>
	<div class="container">
		<div class="page-header">
			<h1>
				게시글 수정 - <small>게시글을 수정하세요.</small>
			</h1>
		</div>

		<form id="frmBoardEdit" class="form-horizontal" role="form"
			method="post" action="editBoardOk.jsp">

			<!-- 게시글 자동입력 -->
			<input type="hidden" name="seq" value="<%=board_seq%>">

			<fieldset>

				<!-- 입력양식 -->

				<div class="form-group">
					<label for="board_category_seq" class="col-md-2 control-label">게시판
						카테고리</label>
					<div class="col-md-10">
						<select id="board_category_seq" name="board_category_seq"
							class="form-control">
							<option>----- 선택하세요 -----</option>
							<%
							if (category_list != null) {
								for (int i=0; i<category_list.size(); i++) {
									BoardCategoryItem item = category_list.get(i);
									if (item.getSeq() == b_item.getBoard_category_seq()) {
						%>
							<option value="<%=item.getSeq()%>" selected><%=item.getCategory_name()%></option>
							<%				
									} else {
						%>
							<option value="<%=item.getSeq()%>"><%=item.getCategory_name()%></option>
							<%
									}
								}
							}
						%>
						</select>
					</div>
				</div>

				<div class="form-group">
					<label for="member_seq" class="col-md-2 control-label">회원</label>
					<div class="col-md-10">
						<select id="member_seq" name="member_seq" class="form-control">
							<option>----- 선택하세요 -----</option>
							<%
							if (member_list != null) {
								for (int i=0; i<member_list.size(); i++) {
									MemberItem item = member_list.get(i);
									if (item.getSeq() == b_item.getMember_seq()) {
						%>
							<option value="<%=item.getSeq()%>" selected><%=item.getUser_name()%>
								/
								<%=item.getEmail1()%>@<%=item.getEmail2()%></option>
							<%				
									} else {
						%>
							<option value="<%=item.getSeq()%>"><%=item.getUser_name()%>
								/
								<%=item.getEmail1()%>@<%=item.getEmail2()%></option>
							<%
									}
								}
							}
						%>
						</select>
					</div>
				</div>

				<div class="form-group">
					<label for="title" class="col-md-2 control-label">제목</label>
					<div class="col-md-10">
						<input type="text" class="form-control" id="title"
							placeholder="제목을 입력하세요" name="title" value=<%=b_item.getTitle()%>
							autofocus>
					</div>
				</div>
				
				<%
				if (category_list != null) {
					for (int i=0; i<category_list.size(); i++) {
						BoardCategoryItem item = category_list.get(i);
						if (item.getSeq() == b_item.getBoard_category_seq() && item.getCategory_name().indexOf("후기") > -1) {
				%>
				<div class="form-group">
					<label for="house_seq" class="col-md-2 control-label">민박</label>
					<div class="col-md-10">
						<select id="house_seq" name="house_seq" class="form-control">
							<option>----- 선택하세요 -----</option>
							<%
							if (house_list != null) {
								for (int j=0; j<house_list.size(); j++) {
									HouseItem house_item = house_list.get(j);
						%>
							<option value="<%=house_item.getSeq()%>" <% if (house_item.getSeq() == b_item.getHouse_seq()) { %>selected<%}%>><%=house_item.getName()%></option>
							<%			
								}
							}
						%>
						</select>
					</div>
				</div>
				
				<div class="form-group">
					<label for="grade" class="col-md-2 control-label">평점</label>
					<div class="col-md-10">
						<select id="grade" name="grade" class="form-control">
							<option>----- 선택하세요 -----</option>
							<option value="5" <%if (b_item.getGrade()==5) { %>selected <%}%>>매우 좋았다.</option>
							<option value="4" <%if (b_item.getGrade()==4) { %>selected <%}%>>만족한다.</option>
							<option value="3" <%if (b_item.getGrade()==3) { %>selected <%}%>>평범하다.</option>
							<option value="2" <%if (b_item.getGrade()==2) { %>selected <%}%>>불만족스럽다.</option>
							<option value="1" <%if (b_item.getGrade()==1) { %>selected <%}%>>최악이다.</option>
						</select>
					</div>
				</div>
				<%			
						}
					}
				}
				%>

				<div class="form-group">
					<label for="content" class="col-md-2 control-label">내용</label>
					<div class="col-md-10">
						<textarea id="content" name="content" class="ckeditor"
							placeholder="내용을 입력하세요"><%=b_item.getContent()%></textarea>
					</div>
				</div>

				<div class="form-group">
					<div class="col-md-offset-2 col-md-10">
						<div class="row">
							<div class="col-md-6">
								<button type="submit" class="btn btn-primary btn-block">수정하기</button>
							</div>
							<div class="col-md-6">
								<button type="reset" class="btn btn-warning btn-block">다시작성</button>
							</div>
						</div>

					</div>
				</div>
			</fieldset>
		</form>
	</div>
	<% db.disconnect(); %>
	<!-- Javascript -->
	<script src="/Team-Project/assets/js/jquery.js"></script>
	<script src="/Team-Project/assets/js/bootstrap.min.js"></script>
	<!-- 플러그인 참조 -->
	<script src="/Team-Project/assets/ckeditor/ckeditor.js"></script>
	<!-- 형식검사 클래스 참조 -->
	<script src="/Team-Project/assets/js/string.js"></script>
	<!-- 구현부분 -->
	<script type="text/javascript">
		// jquery main()
		$(function() {
			$("#frmBoardEdit").submit(function() {
				
				// 게시판 카테고리 선택여부 검사
				if ($("select[name='board_category_seq'] > option:selected").index() < 1) {
					alert("게시판 카테고리를 선택하세요.");
					$("select[name='board_category_seq']").focus();
					return false;
				}
				
				// 회원 선택여부 검사
				if ($("select[name='member_seq'] > option:selected").index() < 1) {
					alert("회원을 선택하세요.");
					$("select[name='member_seq']").focus();
					return false;
				}
				
				// 제목 입력여부 검사
				if (!$("input[name='title']").val().isValue()) {
					alert("제목을 입력하세요.");
					$("input[name='title']").focus();
					return false;
				}
				
				// 내용 입력여부 검사
				if (!$("textarea[name='content']").val().isValue()) {
					alert("내용을 입력하세요.");
					$("input[name='content']").focus();
					return false;
				}
			});
		});
	</script>

</body>
</html>